package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum.StateEnum;
import com.joint.base.entity.Users;
import com.joint.base.service.UsersService;
import com.joint.base.util.DataUtil;
import com.joint.core.entity.ProInfo;
import com.joint.core.entity.ProLeave;
import com.joint.core.service.ProInfoService;
import com.joint.core.service.ProLeaveService;
import com.joint.web.action.BaseAdminAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.Set;

/**
 * Created by xh on 2015/3/2.
 */
@ParentPackage("mp")
public class AjaxProleaveAction extends BaseAdminAction {

    @Resource
    private ProLeaveService proLeaveService;
    @Resource
    private ProInfoService proInfoService;
    @Resource
    private UsersService usersService;

    protected ProLeave proLeave;
    private ProInfo proInfo;

    private String keyId;
    private String proInfoId;

    private String reason;
    private String leaveDate;

    private String error;

    public String read(){
        //LogUtil.info("keyId:" + keyId);
        if(StringUtils.isNotEmpty(keyId)){
            proLeave = proLeaveService.get(keyId);
        }
        return "read";
    }

    public String input(){

        if (StringUtils.isNotEmpty(keyId)){
            proLeave = proLeaveService.get(keyId);
            proInfo = proLeave.getProInfo();
        }else{
            proLeave = new ProLeave();
            if (StringUtils.isNotEmpty(proInfoId)){
                proInfo = proInfoService.get(proInfoId);
                proLeave.setProInfo(proInfo);
            }
        }

        if (StringUtils.isNotEmpty(proInfoId)){
            //如果不是该项目的总监或项目组成员，则不允许新建
            Users users = usersService.getLoginInfo();
            int flg = 0;
            if (users.equals(proInfo.getChief())){
                flg = 1;
            }else{
                Set<Users> usersSet = proInfo.getGroup();
                if(usersSet.size()>0){
                    if(usersSet.contains(users)){
                        flg = 1;
                    }
                }
            }
            if(flg==0){
                error = "您不是该项目的总监或项目组成员，不允许新建！";
                return "error";
                //return ajaxHtmlCallback("404", "您不是该项目的总监或项目组成员，不允许新建！","操作状态");
            }
        }
        return  "input";
    }

    public String save(){
        ProInfo proInfo;
        if (StringUtils.isNotEmpty(proInfoId)){
            proInfo = proInfoService.get(proInfoId);
        }else {
            return ajaxHtmlCallback("404", "所属项目信息表丢失！","操作状态");
        }

        //LogUtil.info("keyId:"+keyId);
        if(StringUtils.isNotEmpty(keyId)){
            proLeave = proLeaveService.get(keyId);
        }else{
            proLeave = new ProLeave();
            proLeave.setCreater(usersService.getLoginInfo());
        }

        proLeave.setProInfo(proInfo);
        proLeave.setName(proInfo.getName());

        proLeave.setReason(reason);
        if (com.joint.base.util.StringUtils.isNotEmpty(leaveDate)){
            proLeave.setLeaveDate(DataUtil.StringToDate(leaveDate + " 12:00", "yyyy-MM-dd HH:mm"));
        }

        proLeave.setCompany(usersService.getCompanyByUser());
        proLeaveService.updateAndEnable(proLeave);
        return ajaxHtmlCallback("200", "保存成功！","操作状态");
    }

    public String delete(){
        if(StringUtils.isEmpty(keyId)){
            return ajaxHtmlCallback("404", "请选择客户信息！","操作状态");
        }
        proLeave = proLeaveService.get(keyId);
        proLeave.setState(StateEnum.Delete);
        proLeaveService.update(proLeave);
        return ajaxHtmlCallback("200", "删除成功！","操作状态");
    }

    @Override
    public String getKeyId() {
        return keyId;
    }

    @Override
    public void setKeyId(String keyId) {
        this.keyId = keyId;
    }

    public String getProInfoId() {
        return proInfoId;
    }

    public void setProInfoId(String proInfoId) {
        this.proInfoId = proInfoId;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getLeaveDate() {
        return leaveDate;
    }

    public void setLeaveDate(String leaveDate) {
        this.leaveDate = leaveDate;
    }

    public ProLeave getProLeave() {
        return proLeave;
    }

    public void setProLeave(ProLeave proLeave) {
        this.proLeave = proLeave;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}
